package com.kitty.game.equip.handler;

import com.kitty.game.config.Equip;
import com.kitty.game.equip.EquipDataPool;
import com.kitty.game.equip.model.RoleEquip;
import com.kitty.game.role.model.Role;
import com.kitty.game.utils.AsktaoUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;

import static com.kitty.game.utils.Const.MONEY_NAME;
import static com.kitty.game.utils.Const.ROLE_RESOURCE_NOT_ENOUGH;
import static com.kitty.game.utils.NotifyModules.NOTIFY_EQUIP_EVOLVE_OK;
import com.kitty.game.equip.handler.UpgradeHandler;

@Component("EquipEvolveHandler")
@Slf4j
public class EquipEvolveHandler extends UpgradeHandler {
    private static final int PLAYER_MAX_LEVEL = 179;  // 人物最高等级
    private static final int REQ_EQUIP_LEVEL = 70;  // 最低壳进化等级
    private static final int EQUIP_EVOLVE_LEVEL_MAX = 19;  //进化最高等级
    private static final String EQUIP_EVOLVE_COLOR = "绿色";  //要求颜色
    private static final String NOT_EQUIP = "请先选择要进化的装备。";
    private static final String NOT_SUIT = "只有套装可以进行进化。";
    private static final String LEVEL_NOT_ENOUGH = "当前装备等级小于70，无法进行进化。";
    private static final String LEVEL_IN_MAX = "当前装备已经进化到最高等级了。";
    private static final String EVOLVE_LEVEL_IN_MAX = "每件装备最多只可进化19次。";
    private static final String LEVEL_GREATER_THAN_ROLE = "当前装备等级大于等于角色等级，无法进行进化。";
    private static final String NEED_STONE = "天星石";
    private static final String SUCCESS = "你花费了#R{0}#n块#R天星石#n及{1}文钱，成功将#R{2}#n进化至#R{3}#n级。";
    private static final String NEED_STONE_NOT_ENOUGH = "你身上天星石不足#R{0}#n块，不能进化。";

    /**
     * 开始打造 返回打造成功或者失败
     *
     * @param role
     * @param params
     */
    @Override
    public String doUpgrade(Role role, Object... params) {
        RoleEquip roleEquip = (RoleEquip) params[0];
        // add tao 防止进化超过135
        if (roleEquip.queryUpgradeLevel() >= 135) {
            return "装备最高进化到#R135#n级";
        }
        // add:e
        Equip evolveEquip = (Equip) params[1];
        boolean useLimit = (boolean) params[2];
        short level = roleEquip.queryLevel();
        int costItem = getEvolveCostItem(level);
        int needCash = needCash(level, 0);

        int limitNum = equipService.delRoloEquip(NEED_STONE, costItem, useLimit, role);
        roleService.subtractMoney(role, needCash);
        if (limitNum > 0) {
            roleEquip.addLimitTime(limitNum);
        }
        /**进化必定成功 成功之后修改需要修改的信息*/
        equipService.alterEquipFromEvolve(roleEquip, evolveEquip);
        roleService.addAchieveScore(role,"301058");
        refreshRoleEquip(role, roleEquip);
        pushOperateResult(role, UPGRADE_SUCCESS);
        return MessageFormat.format(SUCCESS, costItem, AsktaoUtil.getMoneyFormat(needCash), roleEquip.getName(), roleEquip.queryEvolveLevel());
    }


    /**
     * 入口
     *
     * @param role
     * @param upgradePos
     * @param param
     */
    @Override
    public String acceptUpgrade(Role role, int upgradePos, String param) {
        RoleEquip roleEquip = equipService.getRoleEquipByPos(role, upgradePos);
        if (roleEquip == null) {
            return NOT_EQUIP;
        }
        Equip equip = EquipDataPool.getByName(roleEquip.getName());
        if (equip == null) {
            log.error("找不到装备进化配置=[{}]", roleEquip.getName());
            return MessageFormat.format(NOT_FIND_CONFIG, roleEquip.getName());
        }
        /**第十次需要进化装备外观 名字 所以第九次的时候取出下个等级的equip来*/
        Equip evolveEquip = EquipDataPool.getByName(equip.getUpgradeName());
        if (roleEquip.queryEvolveCount() == 9) {
            evolveEquip = EquipDataPool.getByName(equip.getUpgradeName());
            if (evolveEquip == null) {
                log.error("找不到装备进化配置=[{}]", equip.getUpgradeName());
                return MessageFormat.format(NOT_FIND_CONFIG, roleEquip.getName());
            }
        }

        String content = checkResource(role, roleEquip, param.equals("1"));
        if (StringUtils.isNotEmpty(content)) {
            return content;
        }
        return doUpgrade(role, roleEquip, evolveEquip, param.equals("1"));
    }

    /**
     * 检查需要的资源是否足够
     *
     * @param objects
     */
    @Override
    public String checkResource(Object... objects) {
        Role role = (Role) objects[0];
        RoleEquip roleEquip = (RoleEquip) objects[1];
        boolean useLimit = (boolean) objects[2];
        short level = roleEquip.queryLevel();
        if (level < REQ_EQUIP_LEVEL) {
            return LEVEL_NOT_ENOUGH;
        }
        if (level >= role.getLevel()) {
            return LEVEL_GREATER_THAN_ROLE;
        }
        if (!roleEquip.queryColor().equals(EQUIP_EVOLVE_COLOR)) {
            return NOT_SUIT;
        }
        if (level >= PLAYER_MAX_LEVEL) {
            return LEVEL_IN_MAX;
        }
        if (roleEquip.queryEvolveCount() >= EQUIP_EVOLVE_LEVEL_MAX) {
            return EVOLVE_LEVEL_IN_MAX;
        }
        if (needCash(level, 0) > role.getMoney()) {
            return MessageFormat.format(ROLE_RESOURCE_NOT_ENOUGH, MONEY_NAME);
        }
        int stoneNum = equipService.getCount(NEED_STONE, useLimit, role);
        if (getEvolveCostItem(level) > stoneNum) {
            return MessageFormat.format(NEED_STONE_NOT_ENOUGH, getEvolveCostItem(level));
        }
        return null;
    }

    /**
     * 打造需要的金钱
     * pos 只在鉴定的时候用
     *
     * @param level
     * @param pos
     */
    @Override
    public int needCash(short level, int pos) {
        return level * 10000 > 0 ? level * 10000 : 0;
    }

    /**
     * 取操作类型的字符串
     */
    @Override
    public String getUpdateTypeStr() {
        return "equip_evolve";
    }

    int getNotifyType() {
        return NOTIFY_EQUIP_EVOLVE_OK;
    }

    /**
     * 取天星石消耗
     */
    private int getEvolveCostItem(int level) {
        return (int) Math.max(1, Math.floor(level / 10) - 7);
    }
}
